package com.gopay.common.rcs.dao.antiphish.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.constants.rcs.AntiPhishEditNatureEnum;
import com.gopay.common.domain.rcs.RcsAntiPhishConfEditLog;
import com.gopay.common.rcs.dao.antiphish.RcsAntiPhishEditLogDAO;

/**
 * 变更日志 dao实现
 * 
 * @author lwk
 * 
 */
@Repository("rcsAntiPhishEditLogDAO")
public class RcsAntiPhishEditLogDAOImpl extends BaseDAOImpl<RcsAntiPhishConfEditLog, Serializable> implements
        RcsAntiPhishEditLogDAO {

	//private final Logger logger = LoggerFactory.getLogger(RcsAntiPhishEditLogDAOImpl.class);
	
    @Override
    public List<?> findAntiPhishLog(final Map<String, Object> parameterMap,final  int pageIndex,final  int pageSize) {
        final StringBuffer sqlBf = new StringBuffer();
        sqlBf.append("select to_char(l.OPER_TIME, 'yyyy-mm-dd HH24:MI:SS') OPER_TIME, ")//0
            .append(" '' CORP_NAME,                 ")//1
            .append("       l.OPER_CONF_TYPE,       ")//2
            .append("       l.VAL_BEFORE_EDIT,      ")//3
            .append("       l.VAL_AFTER_EDIT,       ")//4
            .append("       l.OPER_NAME,            ")//5
            .append("       l.EDIT_NATURE,          ")//6
            .append("      a1.ANTI_PHISH_TYPE       ")//7
            .append(" from RCS_ANTI_PHISH_CONF_EDIT_LOG l   ")
            .append(" left join RCS_ANTI_PHISH_CONF_GLOBAL a1   ")
            .append("   on l.oper_target_sq = a1.CONF_SQ    ")
            .append("  where l.OPER_CONF_TYPE='01'  ")
            .append(getWhereSql(parameterMap,"golbal"))
            .append("union all ")
            .append("select to_char(l.OPER_TIME, 'yyyy-mm-dd HH24:MI:SS') OPER_TIME,  ")
            .append("      c.CORP_NAME, ")
            .append("      l.OPER_CONF_TYPE,    ")
            .append("      l.VAL_BEFORE_EDIT,   ")
            .append("     l.VAL_AFTER_EDIT, ")
            .append("    l.OPER_NAME,   ")
            .append("     l.EDIT_NATURE,    ")
            .append("      a1.ANTI_PHISH_TYPE   ")
            .append("  from RCS_ANTI_PHISH_CONF_EDIT_LOG l  ")
            .append("  left join RCS_ANTI_PHISH_CONF_CUST a1    ")
            .append("   on l.oper_target_sq = a1.CONF_SQ    ")
            .append("  left join cust_corp_info c   ")
            .append("   on a1.cust_id =c.CUST_ID    ")
            .append("  where l.OPER_CONF_TYPE='02'  ")
            .append(getWhereSql(parameterMap,"cust")).append(" order by OPER_TIME desc ");
            parameterMap.remove("antiPhishTypeEditLog");
            parameterMap.remove("custNameEditLog");
            
            return findBySql(sqlBf.toString(), parameterMap, pageIndex, pageSize);
    }

    private String getWhereSql(Map<String, Object> parameterMap,String type){
        StringBuffer whereSql = new StringBuffer();
        if(StringUtils.isNotBlank((String)parameterMap.get("startDateEditLog"))){
            whereSql.append(" and   to_char(l.OPER_TIME,'YYYY-MM-DD') >=:startDateEditLog ");
        }
        if(StringUtils.isNotBlank((String)parameterMap.get("endDateEditLog"))){
            whereSql.append(" and   to_char(l.OPER_TIME,'YYYY-MM-DD') <=:endDateEditLog ");
        }
        if(StringUtils.isNotBlank((String)parameterMap.get("antiPhishConfTypeEditLog"))){
            whereSql.append(" and   l.OPER_CONF_TYPE=:antiPhishConfTypeEditLog ");
        }
       
            if(StringUtils.isNotBlank((String)parameterMap.get("custNameEditLog"))){
                if(type.equals("cust")){
                    whereSql.append(" and   c.CORP_NAME like '%").append(parameterMap.get("custNameEditLog")).append("%' ");
                }else{
                    //如果按照商户名称查询则不union 全局配置
                    whereSql.append(" and   l.sq=-1 ");
                }
            }
        if(StringUtils.isNotBlank((String)parameterMap.get("operNameEditLog"))){
            whereSql.append(" and   l.OPER_NAME=:operNameEditLog ");
        }
        if(StringUtils.isNotBlank((String)parameterMap.get("antiPhishTypeEditLog"))){
            whereSql.append(getChangeContent(Integer.parseInt((String)parameterMap.get("antiPhishTypeEditLog"))));
        }
        
        return whereSql.toString();
    }
    private String getChangeContent(int antiPhishTypeEditLog){
        StringBuffer whereSql = new StringBuffer();
        switch (antiPhishTypeEditLog) {
        // 域名校验
        case 1:

            whereSql.append(" and  a1.ANTI_PHISH_TYPE='01' and l.EDIT_NATURE='")
                    .append(AntiPhishEditNatureEnum.ANTI_PHISH_TYPE.value).append("' ");
            break;
        // 域名模糊匹配
        case 2:
            whereSql.append(" and  a1.ANTI_PHISH_TYPE='02' and l.EDIT_NATURE='")
                    .append(AntiPhishEditNatureEnum.ANTI_PHISH_TYPE.value).append("' ");
            break;
        // 域名列表
        case 3:
            whereSql.append(" and  a1.ANTI_PHISH_TYPE in('01','02') and l.EDIT_NATURE='")
                    .append(AntiPhishEditNatureEnum.LEGAL_VALUE.value).append("' ");
            break;
        // 时间戳校验
        case 4:
            whereSql.append(" and  a1.ANTI_PHISH_TYPE='03' and l.EDIT_NATURE='")
                    .append(AntiPhishEditNatureEnum.DISJUNCTOR.value).append("' ");
            break;
        // 时间戳差值
        case 5:
            whereSql.append(" and  a1.ANTI_PHISH_TYPE='03' and l.EDIT_NATURE='")
                    .append(AntiPhishEditNatureEnum.LEGAL_VALUE.value).append("' ");
            break;
        // 订单IP校验
        case 6:
            whereSql.append(" and  a1.ANTI_PHISH_TYPE='04' and l.EDIT_NATURE='")
                    .append(AntiPhishEditNatureEnum.ILLEGAL_REQ_DICISION.value).append("' ");
            break;
        // 验证码输入
        case 7:
            whereSql.append(" and  a1.ANTI_PHISH_TYPE='05' and l.EDIT_NATURE='")
                    .append(AntiPhishEditNatureEnum.GATEWAY_CHANNEL.value).append("' ");
            break;
        case 8:
            //  付款到银行ip备案
            whereSql.append(" and  a1.ANTI_PHISH_TYPE='06' and l.EDIT_NATURE='")
            .append(AntiPhishEditNatureEnum.LEGAL_VALUE.value).append("' ");
            break;
        default:
            break;
        }
        return whereSql.toString();
    }
    
    
    @Override
    public int getCount(Map<String, Object> parameterMap) {
        StringBuffer sqlBf = new StringBuffer();
        sqlBf.append("select sum(c) counts from (select count(*) c")//0
            .append(" from RCS_ANTI_PHISH_CONF_EDIT_LOG l   ")
            .append(" left join RCS_ANTI_PHISH_CONF_GLOBAL a1   ")
            .append("   on l.oper_target_sq = a1.CONF_SQ    ")
            .append("  where l.OPER_CONF_TYPE='01'  ")
            .append(getWhereSql(parameterMap,"golbal"))
            .append("union all ")
            .append("select count(*) c")
            .append("  from RCS_ANTI_PHISH_CONF_EDIT_LOG l  ")
            .append("  left join RCS_ANTI_PHISH_CONF_CUST a1    ")
            .append("   on l.oper_target_sq = a1.CONF_SQ    ")
            .append("  left join cust_corp_info c   ")
            .append("   on a1.cust_id =c.CUST_ID    ")
            .append("  where l.OPER_CONF_TYPE='02' ")
            .append(getWhereSql(parameterMap,"cust")).append(" ) ");
        parameterMap.remove("antiPhishTypeEditLog");
        parameterMap.remove("custNameEditLog");
        return findCountBySqlOfHibernate(sqlBf.toString(), parameterMap);
    }
}
